package com.example.shoppingdemo;

import android.app.Application;
import android.util.Log;

import com.example.shoppingdemo.RKL.LoginAndSigin;
import com.example.shoppingdemo.RKL.Persenter.PersenterLoginAngSigin;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class BaseApplication extends Application {
    public static ShoppingRequest ishoppingRequest;
    public static LoginAndSigin loginAndSigin;
    @Override
    public void onCreate() {
        super.onCreate();
        Retrofit retrofit = new Retrofit.Builder()
                //.baseUrl("http://192.168.31.55:52801")
                .baseUrl("http://192.168.31.55:53801")
                //.baseUrl("http://192.168.31.25:53801")
//                .baseUrl("http://192.168.31.55:53803")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .callTimeout(10, TimeUnit.SECONDS)
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(10, TimeUnit.SECONDS)
                        .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                Request request = chain.request();
                                //请求地址
                                HttpUrl url = request.url();
                                Response response = chain.proceed(request);
                                //返回数据
                                String data = response.body().string();
                                int httpCode = response.code();
                                String httpMsg = response.message();
                                MediaType type = response.body().contentType();
                                Log.d("http","请求地址："+url);
                                Log.d("http","返回信息："+httpCode+""+httpMsg);
                                Log.d("http","返回数据："+data);
                                return response.newBuilder().request(request)
                                        .body(ResponseBody.create(type,data))
                                        .build();
                            }
                        })
                        .build()
                )
                .build();
        ishoppingRequest = retrofit.create(ShoppingRequest.class);
        loginAndSigin=retrofit.create(LoginAndSigin.class);
    }
}
